package com.ruoyi.inventory.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.ProductUnit;
import com.ruoyi.inventory.service.IProductUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 产品-计量单位Controller
 *
 * @author xwj
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/inventory/unit")
@Api(tags = "产品-计量单位")
public class ProductUnitController extends BaseController {
    @Autowired
    private IProductUnitService productUnitService;

    /**
     * 查询产品-计量单位列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody ProductUnit productUnit) {
        startPage();
        List<ProductUnit> list = productUnitService.selectProductUnitList(productUnit);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody ProductUnit productUnit) {
        startOrderBy();
        List<ProductUnit> list = productUnitService.selectProductUnitList(productUnit);
        return success(list);
    }

    /**
     * 导出产品-计量单位列表
     */
    @Log(title = "产品-计量单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, ProductUnit productUnit) {
        List<ProductUnit> list = productUnitService.selectProductUnitList(productUnit);
        ExcelUtil<ProductUnit> util = new ExcelUtil<ProductUnit>(ProductUnit.class);
        util.exportExcel(response, list, "产品-计量单位数据");
    }

    /**
     * 获取产品-计量单位详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(productUnitService.selectProductUnitById(id));
    }

    /**
     * 新增产品-计量单位
     */
    @Log(title = "产品-计量单位", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody ProductUnit productUnit) {
        SysUser user = getLoginUser().getUser();
        if(productUnit.getId() == null) {
            productUnit.setCreateId(user.getUserId());
            productUnit.setCreateTime(DateUtils.getNowDate());
            productUnit.setCreateBy(user.getNickName());
            productUnitService.insertProductUnit(productUnit);
        } else {
            productUnit.setUpdateId(user.getUserId());
            productUnit.setUpdateTime(DateUtils.getNowDate());
            productUnit.setUpdateBy(user.getNickName());
            productUnitService.updateProductUnit(productUnit);
        }

        return success(productUnit);
    }

    /**
     * 修改产品-计量单位
     */
    @Log(title = "产品-计量单位", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody ProductUnit productUnit) {
        SysUser user = getLoginUser().getUser();
        productUnit.setUpdateId(user.getUserId());
        productUnit.setUpdateTime(DateUtils.getNowDate());
        productUnit.setUpdateBy(user.getNickName());
        return success(productUnitService.updateProductUnit(productUnit));
    }

    /**
     * 删除产品-计量单位
     */
    @Log(title = "产品-计量单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productUnitService.deleteProductUnitByIds(ids));
    }

    @Log(title = "产品-计量单位", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody ProductUnit productUnit) {
        int num = productUnitService.delete(productUnit);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<ProductUnit>> queryPage(@RequestBody ProductUnit productUnit) {
        Pages<ProductUnit> page = productUnitService.queryPage(productUnit);
        return ResponseModel.of(page);
    }
}
